
import { jsonForm } from "../utils/util.js"

const POST = (url, postData, doSuccess, doFail, doComplete) => {
  wx.showLoading({
    title: '数据请求中...',
  })
  wx.request({
    url: url,
    data: jsonForm(postData),
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    success: function (res) {
      console.log(res)
      wx.hideLoading();
      if (res.statusCode == 200 && typeof doSuccess == "function") {
        doSuccess(res);
      } else {
        wx.showToast({
          title: "数据走丢啦",
          icon: 'none'
        })
        return;
      }
    },
    fail: function () {
      wx.hideLoading();
      if (typeof doFail == "function") {
        doFail();
      }
      wx.showToast({
        title: "网络异常，请稍后再试",
        icon: 'none'
      })
    },
    complete: function () {
      if (typeof doComplete == "function") {
        doComplete();
      }
    }
  });
}

const GET = (url, postData, doSuccess, doFail, doComplete) => {
  wx.showLoading({
    title: '数据请求中...',
  })
  wx.request({
    url: url,
    data: postData,
    header: {
      'content-type': 'application/json',
      'token': '47FD11E89D8764CB0180550D1D8370B7'
    },
    success: function (res) {
      wx.hideLoading();
      if (res.statusCode == 200 && typeof doSuccess == "function") {
        doSuccess(res);
      } else {
        wx.showToast({
          title: "数据走丢啦",
          icon: 'none'
        })
        return;
      }
    },
    fail: function () {
      wx.hideLoading();
      if (typeof doFail == "function") {
        doFail();
      }
      wx.showToast({
        title: "网络异常，请稍后再试",
        icon: 'none'
      })
    },
    complete: function () {
      if (typeof doComplete == "function") {
        doComplete();
      }
    }
  });
}

export { POST, GET }  